var jsonList = {};
var page = 1;
var rows = 10;
var showBok=false;
var bxlist = [];
//添加
 $(".xz").click(function(){
    $('.shouye').hide();
	$('.tab-box').show();
	 $(".btn_xiugai").hide();
	 //$.FreeTags('tags');
	 $('#form-tags-4').tagsInput({
		 'autocomplete': {
			 source: zt_biao
		 }
	 });

});
//修改
$(".xg").on('click',function(){
	
        var numCount = 0;
		var ids = '';
		$(".arc_isc").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid");
			}
		})
		if (numCount == 1) {
			$('.shouye').hide();
			$(".btn_xiugai").show();
			$(".btn_title_chuang").hide();
			$('.tab-box').show();
			//进行下一步
			console.log(ids,898989);
			var json = {}
			var result = JSON.stringify(json);
			$.ajax({
			url: baseNews +"/api/data/article/info",
			data:{
				id: ids
			},
			success:function (res){
				console.log(res,7878)
				if(res.code==0){
					var result=res.data;
					console.log(result,1010);
					$(".wz_id").val(ids);
				 $(".wz_name1").val(result.title);//文章名称
					var tags = "";
					for (var i = 0; i < result.tagArray.length; i++) {

						for (var j = 0; j < bxlist.length; j++) {
							if (result.tagArray[i] == bxlist[j].id) {
								tags = tags + bxlist[j].tag + ",";
							}
						}
						// if (bxlist[i] == cont) {
						// 	tags = tags + bxlist[i].id + ","
						// }
					}
					$(".add_biaoq").attr("value", tags);
					$('#form-tags-4').tagsInput({
						'autocomplete': {
							source: zt_biao
						}
					});
					var imgArr=result.images;
					if(imgArr.length>0){
						var html="";
						$(imgArr).each(function (index,key){
							html+=`<div style="float:left;background:#f3f3f3;margin-right:30px;width: 160px;height: 160px;position: relative;">
							<a  class="label" >
								<img src="${key}"  class="img0" >
							</a>
						</div>`;
						
						})
						$(".show_tHimg").append(html);
					}
				//var isTop = $(".arc_top").prop("checked");//是否置顶
				//var arcImg = $(".tu img").attr("fsrc")||"";//封面图
				//var arcid = $(".ztid_text").val();//专题id
				$(".ly_text").val(result.source);//来源
				var wz_text = $(".wz_text").val(result.url);//网址
				var zj_text = $(".zj_text").val(result.author);//作者
				//var mz_checkbox = $(".mz_checkbox").prop("checked");//免责声明
				$(".fbs_text").val(result.stime);//发布时间
				//$(".bzt_id").val(result.sid);//专题id
				$('#lunch').val([result.sid]);
				$('#lunch').selectpicker('refresh');
				$('#lunch').selectpicker('render'); 
				var  checkOid=(result.type);
				//alert(checkOid);
					var sp=$(".arc_lb").children("option");
					 $(sp).each(function(index,key){
						 if ($(this).attr("value") == checkOid){
							$(this).attr("selected",'selected')
						}
					 }) 
				//$(".arc_pindao").val(result.id);//频道id
				//$(".arc_lb").val(result.type);//类别
				//var scont = editor.txt.html();//文章内容
				$(".t_name").val(result.title);
				$(".b_bianqian").attr("value",result.tags);
				//$.FreeTags('tags');
				editor.txt.html(`<div>${result.content}</div>`);
					// //频道的默认选中
					// var checkOid = (result.cid);
					// var sp = $(".arc_pindao").children("option");
					// $(sp).each(function (index, key) {
					// 	if ($(this).attr("value") == checkOid) {
					// 		$(this).attr("selected", 'selected')
					// 	}
					// })
					//$(".t_top").prop("checked");
					if(result.top==true){
						$(".arc_top").prop("checked",true);
					}
					if(result.statement==true){
						$(".mz_checkbox").prop("checked",true);
					}
					$(".t_jianjie").val(result.synopsis);
					if (result.cover!=""){
						$(".tu img").attr("src", result.cover);
						$(".tu img").attr("fsrc", result.cover);
					}

					
				}
			}
		})
			
		} else {
			//遮罩层提示
			zeroModal.alert("请选择一条数据进行修改");

		}

    	
});
//获取默认列表
getList();
function getList() {
	var json={};
	var result = JSON.stringify(jsonList);
	$.ajax({
		url: "http://news.zrart.net.cn/news/api/data/article/list?page=" + page + "&rows=" + rows,
		type: "post",
		headers: { 'Content-Type': 'application/json' },
		dataType: "json",
		data: result,
		success: function (res) {
			console.log(res, 666);
			if (res.code == 0) {
				var result = res.data;
				var list = result.list;
				console.log(list,9999)
				var html = "";
				$("#page").paging({
					pageNo: page,
					totalPage: result.totalpage,
					totalSize: result.totals,
					callback: function (num) {
						page = num;
						getList();
					}
				})
				$(".arc_list").empty();
				$(list).each(function (index, key) {
					var state = key.state;//文章的状态
					var state1 = key.sstate//专题的状态
					var newState = "";//文章的
					var newState1="";//专题的
					if (state == 0) {
						newState = "文章";
					}
					if (state == 1) {
						newState = "已修改";
					}
					if (state == 2) {
						newState = "待审核";
					}
					if (state == 3) {
						newState = "待发布";
					}
					if (state == 4) {
						newState = "已发布";
					}
					if (state == 10) {
						newState = "新匹配";
					}
					if (state == -10) {
						newState = "已忽略";
					}
					if (state == -1) {
						newState = "审核失败";
					}
					if (state == -2) {
						newState = "撤销发布";
					}
					//
					if (state1 == 0) {
						newState1 = "";
					}
					if (state1 == 1) {
						newState1 = "待审核";
					}
					if (state1 == 2) {
						newState1 = "待发布";
					}
					if (state1 == 3) {
						newState1 = "已发布";
					}
					if (state1 == 10) {
						newState1 = "有更新";
					}
					if (state1 == -1) {
						newState = "审核失败";
					}
					if (state1 ==-2) {
						newState1 = "撤销发布";
					}
					html += `<tr >
							<td >
								<input type="checkbox" oid="${key.id}" class="arc_isc"/>
							</td>
							<td >${key.id}</td>
							<td >${key.title}</td>
							<td >${newState}</td>
							<td >${key.sid}</td>
							<td >${key.stitle == null ? '' : key.stitle}</td>
							<td >${newState1}</td>
							<td >${key.cname == null ?'': key.cname}</td>
							<td >${key.type?key.type:''}</td>
							<td >${key.top==true?"是":"否"}</td>
							<td >${key.recommend == true ? "是" : "否"}</td>
							<td >${key.diy==true?"是":"否"}</td>
							<td >${key.statement==true?"有":"无"}</td>
							<td >${key.click?key.click:""}</td>
							<td >${key.collect?key.collect:''}</td>
							<td >${key.pass?key.pass:""}</td>
							<td >${key.comment?key.comment:''}</td>
							<td >${key.hot?key.hot:""}</td>
							<td >${key.username?key.username:""}</td>
							<td >${key.createtime?key.createtime:''}</td>
							<td >${key.updatetime?key.updatetime:''}</td>
							<td >${key.atime?key.atime:''}</td>
							<td >${key.rtime?key.rtime:''}</td>
							<td >${key.source?key.source:''}</td>
							<td >${key.author?key.author:''}</td>
							<td >${key.stime?key.stime:''}</td>
							<td >xxx</td>
						</tr>`;
				})
				$(".arc_list").append(html);
			}
		}

	})
	//分页

}
$(function (){
	//图片上传预览
	$('.file').on('change', function (e) {
		var e = e || event;
		var reader = new FileReader();
		console.log(reader)
		reader.readAsDataURL($('.file')[0].files[0]);//发起异步请
		//上传图片1
		sendImg('.tu img', $('.file')[0].files[0])
		reader.onload = function () {
			var src = this.result
			$('.tu img').attr('src', src)
		}
	})
	//获取专题id
	$.ajax({
		url: baseNews + "/api/data/article/special/select",
		success: function (res) {
			console.log(res);
			if (res.code == 0) {
				var obj = $(".select_zt");
				var obj1 = $(".atc_ztid");
				$(res.data).each(function (index, key) {
					$('.selectpicker').append("<option value='" + key.id + "'>" + key.value + "</option>");
					obj1.append("<option value='" + key.id + "'>" + key.value + "</option>");
				})
				$('#lunch').selectpicker('refresh');
				$('#lunch').selectpicker('render');  
			}
		}
	})
	//获取频道列表
	$.ajax({
		url: baseNews + "/api/data/special/channel/select",
		success: function (res) {
			console.log(res);
			if (res.code == 0) {
				var obj = $(".arc_pindao");
				var obj1 = $(".arc_pd");
				$(res.data).each(function (index, key) {
					obj.append("<option value='" + key.id + "'>" + key.value + "</option>");
					obj1.append("<option value='" + key.id + "'>" + key.value + "</option>");
				})
			}
		}
	})
	//文章删除
	$('.arc_sc').click(function () {
		var numCount = 0;
		var ids = '';
		$(".arc_isc").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要删除该文章吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "/api/data/article/del?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('删除文章失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条文章删除');

		}
	})
	//文章置顶
	$(".arc_zd").click(function () {
		var numCount = 0;
		var ids = '';
		$(".arc_isc").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要置顶该文章吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "/api/data/article/top/true?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('置顶文章失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条文章置顶');

		}
		//return false;
	})
	//文章取消置顶
	$(".arc_zdqx").click(function () {
		var numCount = 0;
		var ids = '';
		$(".arc_isc").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要取消置顶该文章吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "/api/data/article/top/false?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('取消文章置顶失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条文章取消置顶');

		}
		//return false;
	})
	//文章推荐
	$(".arc_tj").click(function () {
		var numCount = 0;
		var ids = '';
		$(".arc_isc").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要推荐该文章吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "/api/data/article/recommend/true?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('推荐文章失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条文章推荐');

		}
		//return false;
	})
	//文章取消推荐
	$(".arc_qxtj").click(function () {
		var numCount = 0;
		var ids = '';
		$(".arc_isc").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要取消推荐该文章吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "/api/data/article/recommend/false?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('取消推荐文章失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条文章取消推荐');

		}
		//return false;
	})
	//添加免责声明
	$(".arc_addm").click(function (){
		var numCount = 0;
		var ids = '';
		$(".arc_isc").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要添加免责声明吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "/api/data/article/statement/true?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('添加免责声明失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条文章添加免责声明');

		}
	})
	//删除免责声明
	//添加免责声明
	$(".arc_addmqx").click(function () {
		var numCount = 0;
		var ids = '';
		$(".arc_isc").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要删除免责声明吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "/api/data/article/statement/false?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('删除免责声明失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条文章删除免责声明');

		}
	})
	//搜索
	$(".arc_sele").change(function () {
		jsonList.state = $(this).val();
	})
	$(".atc_ztid").change(function () {
		jsonList.sid = $(this).val();
	})
	$(".arc_pd").change(function () {
		jsonList.cid = $(this).val();
	})
	$(".arc_sou").click(function (){

		var arc_name = $(".arc_name").val();
		var arc_sele = $(".arc_sele").val();//文章状态
		console.log(arc_sele,333)
		if (arc_name!= "") {
			jsonList.title = arc_name
		}
		
		//var atc_ztid = $(".atc_ztid").val()//专题
		page = 1;
		getList();
	})

})
// 图片上传的。。
function sendImg(obj, msrc) {
	var formData = new FormData();
	formData.append('file', msrc);
	$.ajax({
		url: baseNews + '/oss/upload/img',
		data: formData,
		type: "post",
		cache: false,
		processData: false,
		contentType: false,
		success: function (res) {
			console.log(res);
			if (res.code == 0) {
				$(obj).attr("fsrc", res.data);
			} else {
				alert("上传失败");
				return false;
			}
		}

	})
}
$(".btn_title_chuang").click(function (){
	var arctitle = $(".wz_name1").val()||"";//文章名称
	var isTop = $(".arc_top").prop("checked");//是否置顶
	var arcImg = $(".tu img").attr("fsrc")||"";//封面图
	//var arcid = $(".ztid_text").val();//专题id
	var ly_text = $(".ly_text").val();//来源
	var wz_text = $(".wz_text").val();//网址
	var zj_text = $(".zj_text").val();//作者
	var mz_checkbox = $(".mz_checkbox").prop("checked");//免责声明
	var fbs_text = $(".fbs_text").val();//发布时间
	var zq_se = $("#lunch").val();//专题id
	alert(zq_se);
	var arc_pindao = $(".arc_pindao").val();//频道id
	var arc_lb = $(".arc_lb").val();//类别
	var scont = editor.txt.html();//文章内容
	var tags = "";
	// $(".input_box a").each(function (index, key) {
	// 	tags = tags + $(this).find("span").html() + ',';
	// })
	$(".tag").each(function (index, key) {
		var cont = $(this).find("span").html();
		for (var i = 0; i < bxlist.length; i++) {
			if (bxlist[i].tag == cont) {
				tags = tags + bxlist[i].id + ","
			}
		}

	})
	tags = tags.substring(0, tags.length - 1);
	if (editor.txt.text()==""){
		zeroModal.alert('请填写文章内容');
		return false;
	}
	if (arcImg==""){
		zeroModal.alert('请选择封面图片');
		return false;
	}
	if (arctitle==""){
		zeroModal.alert('请输入标题');
		return false;
	}
	// if (arc_lb==null){
	// 	zeroModal.alert('请输入类别');
	// 	return false;
	// }
	var json = {
		"author": zj_text,
		"content":scont,
		"cover": arcImg,
		"sid": zq_se,
		"source": ly_text,
		"statement": mz_checkbox,
		
		"tags": tags,
		"title": arctitle,
		"top": isTop,
		"type": arc_lb,
		"url": wz_text
	}
	if (fbs_text!=""){
		json.stime = fbs_text + ":00"
	}
	console.log(json,9999)
	var result = JSON.stringify(json);
	zeroModal.loading(5);
	$.ajax({
		url: baseNews +"/api/data/article/add",
		type: "post",
		headers: { 'Content-Type': 'application/json'},
		dataType: "json",
		data: result,
		success:function (res){
			console.log(res,666)
			if(res.code==0){
				location.reload();
			}else{

				zeroModal.alert('添加失败');

			}
		}
	})
})

$(function (){
	//修改详情
	$(".btn_xiugai").on("click",function (){
		var arctitle = $(".wz_name1").val() || "";//文章名称
		var isTop = $(".arc_top").prop("checked");//是否置顶
		var arcImg = $(".tu img").attr("fsrc") || "";//封面图
		//var arcid = $(".ztid_text").val();//专题id
		var ly_text = $(".ly_text").val();//来源
		var wz_text = $(".wz_text").val();//网址
		var zj_text = $(".zj_text").val();//作者
		var mz_checkbox = $(".mz_checkbox").prop("checked");//免责声明
		var fbs_text = $(".fbs_text").val()||"";//发布时间
		var zq_se = $("#lunch").val();//专题id
		var arc_pindao = $(".arc_pindao").val();//频道id
		var arc_lb = $(".arc_lb").val();//类别
		var scont = editor.txt.html();//文章内容
		var tags = "";
		// $(".input_box a").each(function (index, key) {
		// 	tags = tags + $(this).find("span").html() + ',';
		// })
		$(".tag").each(function (index, key) {
			var cont = $(this).find("span").html();
			for (var i = 0; i < bxlist.length; i++) {
				if (bxlist[i].tag == cont) {
					tags = tags + bxlist[i].id + ","
				}
			}

		})
		tags = tags.substring(0, tags.length - 1);
		//alert(tags)
		if (editor.txt.text() == "") {
			zeroModal.alert('请填写文章内容');
			return false;
		}
		if (arcImg == "") {
			zeroModal.alert('请选择封面图片');
			return false;
		}
		if (arctitle == "") {
			zeroModal.alert('请输入标题');
			return false;
		}
		if (arc_lb == null) {
			zeroModal.alert('请输入类别');
			return false;
		}
		if(fbs_text!=""){
			fbs_text+=":00";
		}
		var json = {
			"author": zj_text,
			"content": scont,
			"cover": arcImg,
			"id":$(".wz_id").val(),
			"sid": zq_se,
			"source": ly_text,
			"statement": mz_checkbox,
			"stime": fbs_text,
			"tags": tags,
			"title": arctitle,
			"top": isTop,
			"type": arc_lb,
			"url": wz_text
		}
		console.log(json, 9999)
		var result = JSON.stringify(json);
		zeroModal.loading(5);
		$.ajax({
			url: baseNews + "/api/data/article/update",
			type: "post",
			headers: { 'Content-Type': 'application/json' },
			dataType: "json",
			data: result,
			success: function (res) {
				console.log(res, 666)
				if (res.code == 0) {
					location.reload();
				} else {

					zeroModal.alert('添加失败');

				}
			}
		})
	})
})
$(function () {
	//获取专题的标签详情
	$.ajax({
		url: baseNews + "/api/data/article/tag/list",
		success: function (res) {
			console.log(res, 666);
			var result = res.data;
			var arr = [];
			bxlist = result;
			arr = result.map(function (key, index) {
				return `${key.tag}`;
			})
			console.log(arr,678)
			zt_biao = arr;


		}
	})
})
$(function (){
	//获取文章类别
	$.ajax({
		url: baseNews + "/api/data/article/types",
		success: function (res) {
			console.log(res);
			if (res.code == 0) {
				var obj = $(".arc_lb");
				$(res.data).each(function (index, key) {
					obj.append("<option value='" + key.id + "'>" + key.value + "</option>");
				})
			}
		}
	})
})